export const getYearMonthDayArr = date => {

    let d = date
    let month = '' + (d.getMonth() + 1)
    let day = '' + d.getDate()
    let year = d.getFullYear()

    if (month.length < 2) month = '0' + month
    if (day.length < 2) day = '0' + day

    // const result = [year, month, day].join('-')
    return [year, month, day]
}

export const formatDate = (date, s = '') => {
    const result = getYearMonthDayArr(date).join(s)
    return result
}

export const applyDateDiff = (date, diff) => {
    const o = {
        year: date.getFullYear(),
        month: date.getMonth(),
        day: date.getDate(),
        hour: date.getHours(),
        m: date.getMinutes(),
        s: date.getSeconds(),
        ms: date.getMilliseconds(),
    }

    const result = new Date(o.year, o.month, o.day, o.hour, o.m, o.s, o.ms)
    const mapper = {
        'year': 'setFullYear',
        'month': 'setMonth',
        'day': 'setDate',
        'hour': 'setHours',
        'm': 'setMinutes',
        's': 'setSeconds',
        'ms': 'setMilliseconds',
    }
    const keys = Object.keys(diff)
    for (let i = 0; i < keys.length; i++) {
        let key = keys[i]
        let method = mapper[key]
        let gap = diff[key]
        let cur = o[key]
        console.log(method)
        console.log(cur, gap)
        if (cur === 0 || gap === 0) {
            return result
        } else {
            result[method](cur + gap)
        }

    }
    return result
}

export const formatDateWithDot = date => {
    let d = date
    let month = '' + (d.getMonth() + 1)
    let day = '' + d.getDate()
    let year = d.getFullYear()

    if (month.length < 2) month = '0' + month
    if (day.length < 2) day = '0' + day

    const result = [year, month, day].join('-')

    return result
}

export const formatScheduleDate = schedule => {
    let scheduleTime = schedule
        .toTimeString()
        .split(' ')[0]
        .split(':')
        .slice(0, 2)
        .join(':')
    return scheduleTime
}

export const handleDateChange = (dateString, diff, split = '') => {
    dateString = dateString.split(split).join('')
    const year = dateString.slice(0, 4)
    const month = dateString.slice(4, 6)
    const day = dateString.slice(6)
    dateString = [year, month, day].join('-')
    const date = new Date(dateString)
    const result = new Date(date.setDate(date.getDate() + diff))
    console.log(result)
    return result
}

export const getWeekDayFromDateString = (dateString) => {
    const date = new Date(dateString)
    const day = date.getDay()
    const arr = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六',]
    return arr[day]
}

export const getDifferenceBetweenDates = (timeObj1, timeObj2) => {

    const d1 = timeObj1.getTime()
    const d2 = timeObj2.getTime()
    const dateDiff = d2 - d1 // 毫秒
    const dayDiff = Math.floor(dateDiff / (24 * 3600 * 1000))
    const leave1 = dateDiff % (24 * 3600 * 1000)
    const hoursDiff = Math.floor(leave1 / (3600 * 1000))
    const leave2 = leave1 % (3600 * 1000)
    const minutesDiff = Math.floor(leave2 / (60 * 1000))
    return {
        dayDiff,
        hoursDiff,
        minutesDiff,
    }
}

export const splitDateWithHr = date => {
    const year = date.slice(0, 4)
    const month = date.slice(4, 6)
    const day = date.slice(6)
    const dateString = `${year}-${month}-${day}`
    return dateString
}

export const compareDateString = (date1, date2) => {

    let time1 = new Date(splitDateWithHr(date1))
    let time2 = new Date(splitDateWithHr(date2))

    // console.log(time1 - time2)

    return time1 - time2

}

// export const dateStringTo
